//
//  DateVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "DateVC.h"
#import "ScrollView.h"
#import "XYuser.h"
#import "UserDetailVC.h"

@interface DateVC () <ScrollViewDelegate, UserDetailDelegate>
{
    ScrollView *pViewShowUsers;
    NSMutableArray *dataArr;
    
    int pIntPage;
}

@property (weak, nonatomic) IBOutlet UILabel *pLblNickName;
@property (weak, nonatomic) IBOutlet UIImageView *pImgUserSex;

@end

@implementation DateVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    pIntPage = 2;
    
    pViewShowUsers = [[ScrollView alloc] initWithFrame:self.view.bounds];
    [pViewShowUsers setPDelegate:self];
    [self.view addSubview:pViewShowUsers];
    
    [self requestData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiReload) name:@"reloadSeeList" object:nil];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    // Do any additional setup after loading the view.
}

- (void)notiReload {
    [self btnReloadListAction:nil];
}

-(void)requestData {
    if (!dataArr) {
        dataArr = [NSMutableArray array];
    }
    User *user = [User defaultUser];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *strSex = @"3";
    if ([[userDefault objectForKey:kUserShowBoyOrGirl] intValue] > 0) {
        strSex = [userDefault objectForKey:kUserShowBoyOrGirl];
    }
    
    [TDNetwork requestFirstUserListPostUrl:GetWantSeeUseListMessageIP page:pIntPage pageSize:10 curJd:[user.pStrLongitude doubleValue] curWd:[user.pStrLatitude doubleValue] userSex:strSex complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            NSArray *arr = [[dictionary valueForKey:@"res"] valueForKey:@"userList"];
            NSLog(@"附近用户信息： %@", dictionary);
            for (NSDictionary *dic in arr)
            {
                XYuser *xyuser = [[XYuser alloc] init];
                
                xyuser.appointmentCount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appointmentCount"]];
                xyuser.beReportedCount  = [NSString stringWithFormat:@"%@", [dic valueForKey:@"beReportedCount"]];
                xyuser.breakCount       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"breakCount"]];
                xyuser.distance         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"distance"]];
                xyuser.faceScore        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"faceScore"]];
                xyuser.fans             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"fans"]];
                xyuser.headImage        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"headImage"]];
                xyuser.movieLabel       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieLabel"]];
                xyuser.nickname         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickName"]];
                xyuser.phone            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                xyuser.pwd              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"pwd"]];
                xyuser.regTime          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"regTime"]];
                xyuser.saltCode         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"saltCode"]];
                xyuser.uid              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                xyuser.userAge          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAge"]];
                xyuser.userSex          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userSex"]];
                xyuser.zambia           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"zambia"]];
                xyuser.address          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"address"]];
                xyuser.albumNum         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"albumNum"]];
                xyuser.charmValue       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"charmValue"]];
                xyuser.commentCount     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentCount"]];
                xyuser.commentStart     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentStart"]];
                xyuser.commentTotalScore= [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentTotalScore"]];
                xyuser.type             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
                xyuser.activeTime       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"activeTime"]];
                
                NSString *signature = [dic valueForKey:@"signature"];
                if (signature && [signature isKindOfClass:[NSString class]]) {
                    xyuser.signature   = [NSString stringWithFormat:@"%@", signature];
                } else {
                    xyuser.signature = @"这个人很懒什么都没写";
                }
                
                NSString *labels =  [dic valueForKey:@"labels"];
                @try {
                    [labels stringByReplacingOccurrencesOfString:@"\\U" withString:@"!u"];
                    [labels stringByReplacingOccurrencesOfString:@"\\" withString:@""];
                    [labels stringByReplacingOccurrencesOfString:@"!u" withString:@"\\u"];
                    NSString*labels2 =  [labels stringByReplacingOccurrencesOfString:@"\'" withString:@"\""];
                    NSArray *arr=  [DateVC arrWithJsonString:labels2];
                    for (NSDictionary *dic in arr) {
                        MovieLabelModel *movieLbM = [[MovieLabelModel alloc]init];
                        movieLbM.name = [dic valueForKey:@"name"];
                        movieLbM.type = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
                        movieLbM.uuid = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uuid"]];
                        [xyuser.labels addObject:movieLbM];
                    }
                } @catch (NSException *exception) {
                } @finally {
                }
                
                [dataArr addObject:xyuser];
            }
            
            if (arr.count > 0) {
                pIntPage ++;
            }
            [pViewShowUsers setPArrayData:dataArr];
        } else {
            [SVProgressHUD showErrorWithStatus:@"获取失败"];
        }
    }];
}

+ (NSMutableArray *)arrWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData
                                                          options:NSJSONReadingMutableContainers
                                                            error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return arr;
}

- (IBAction)btnLikeAction:(UIButton *)sender {

    [pViewShowUsers selSelectMoveWay:em_right];
}

- (IBAction)btnDislikeAction:(UIButton *)sender {

    [pViewShowUsers selSelectMoveWay:em_left];
}

- (IBAction)btnReloadListAction:(id)sender {
    [pViewShowUsers selRloadUserList];
    [self requestData];
}

#pragma mark - ScrollViewDelegate

- (void)selShowUserInfo:(XYuser *)user
{
    [self.pLblNickName setText:user.nickname];
    if ([user.userSex isEqualToString:@"1"]) {
        [_pImgUserSex setImage:[UIImage imageNamed:@"sex_boy"]];
    } else {
        [_pImgUserSex setImage:[UIImage imageNamed:@"sex_girl"]];
    }
}

- (void)selWillReloadData {
    [self requestData];
}

- (void)selDidSelectRefresh {
    pIntPage = 1;
    [self requestData];
}

- (void)selSelectUser:(XYuser *)user {
    [self performSegueWithIdentifier:@"idfUserDetailVC" sender:user];
}

#pragma mark - UserDetailDelegate

- (void)selLike {
    [self btnLikeAction:nil];
}

- (void)selDislike {
    [self btnDislikeAction:nil];
}

#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idfUserDetailVC"]) {
        UserDetailVC *nextVC = segue.destinationViewController;
        nextVC.pUser = sender;
        nextVC.pDelegate = self;
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

@end
